package com.acce.gxsc.page.outCar.detail;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.acce.gxsc.R;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.netBean.NetBatchBean;
import com.acce.gxsc.model.netBean.NetInBatchListBean;
import com.acce.gxsc.model.netBean.NetIncarInfoBean;
import com.acce.gxsc.page.outCar.detail.batchDetail.OutCarBatchDetailActivity;
import com.acce.gxsc.page.supervision.makeupCar.SupervisionMakeupCarActivity;
import com.acce.sdk.base.mvp.BaseMvpActivity;
import com.acce.sdk.utils.StringUtils;
import com.acce.sdk.utils.ToastUtils;
import com.acce.sdk.widget.AcceBar;
import com.acce.sdk.widget.adapterrv.ViewHolder;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by huxley on 2017/5/26.
 */

public class OutCarDetailActivity extends BaseMvpActivity<OutCarDetailView, OutCarDetailPresenter> implements OutCarDetailView {

    String queryId = "";
    boolean        isOutCar;
    LayoutInflater inflater;
    ViewHolder     holder;

    @Bind(R.id.ll_root)
    LinearLayout mLlRoot;
    @Bind(R.id.accebar)
    AcceBar      mAccebar;

    @Override
    protected OutCarDetailPresenter createPresenter() {
        return new OutCarDetailPresenter();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.out_car_detail_activity);
        ButterKnife.bind(this);
        inflater = LayoutInflater.from(this);

        if (getIntent().hasExtra("queryId")) {
            queryId = getIntent().getStringExtra("queryId");
        }
        if (getIntent().hasExtra("isOutCar")) {
            isOutCar = getIntent().getBooleanExtra("isOutCar", false);
        }
        mAccebar.setTitleText(isOutCar ? "车辆离场核查页面" : "车辆监察审核页面");
        holder = new ViewHolder(this, mLlRoot);
        mPresenter.loadOutCarDetail(queryId);
    }

    private void setData(final NetInBatchListBean inBatchListBean) {
        if (inBatchListBean != null) {
            final NetIncarInfoBean incarInfo = inBatchListBean.incarInfo;
            if (incarInfo != null) {
                holder.setText(R.id.tv_in_car_id, Tools.getString(incarInfo.inCarId));
                holder.setText(R.id.tv_dilivery_type, Tools.getString(incarInfo.diliveryType));
                holder.setText(R.id.tv_plate_num, Tools.getString(incarInfo.plateNum));
                holder.setText(R.id.tv_contain_no, Tools.getString(incarInfo.containNo));
                holder.setText(R.id.tv_hang_num, Tools.getString(incarInfo.hangNum));
                holder.setText(R.id.tv_create_time, Tools.getTime(incarInfo.createTime));
                holder.setText(R.id.tv_stay_time, Tools.getStayTime1(incarInfo.createTime));
                holder.setText(R.id.tv_park_area, Tools.getString(incarInfo.parkArea));
                holder.setText(R.id.tv_confirm_gate_name, Tools.getString(incarInfo.confirmGateName));
                holder.setText(R.id.tv_confirm_registrant, Tools.getString(incarInfo.confirmRegistrant));
                holder.setText(R.id.tv_confirm_register_time, Tools.getTime(incarInfo.confirmRegisterDate, incarInfo.confirmRegisterTime));
                holder.setText(R.id.tv_in_car_gate_no, Tools.getString(incarInfo.inCarGateNo));
                holder.setText(R.id.tv_in_car_registrant, Tools.getString(incarInfo.inCarRegistrant));
                holder.setText(R.id.tv_create_time_1, Tools.getTime(incarInfo.createTime));
                holder.setText(R.id.tv_supervise, Tools.getSuperviseOthers(incarInfo.superviseOther));
                holder.setText(R.id.tv_supervise_registrant, Tools.getString(incarInfo.superviseRegistrant));
                holder.setText(R.id.tv_supervise_register_time, Tools.getTime(incarInfo.superviseRegisterDate, incarInfo.superviseRegisterTime));
                holder.setText(R.id.tv_supervise_remarks, Tools.getString(incarInfo.superviseRemarks));
                holder.setText(R.id.tv_out_car, Tools.getOutCarStatus(incarInfo.outCarStatus));
                holder.setText(R.id.tv_out_car_gate_no, Tools.getString(incarInfo.outCarGateNo));
                holder.setText(R.id.tv_out_car_register_time, Tools.getTime(incarInfo.outCarRegisterDate, incarInfo.outCarRegisterTime));
                holder.setText(R.id.tv_out_car_remarks, Tools.getString(incarInfo.outCarRemarks));
                holder.setText(R.id.tv_car_weight, Tools.getString(incarInfo.carWeight));
                holder.setText(R.id.tv_car_type, Tools.getString(incarInfo.carType));
                LinearLayout llContent = holder.getView(R.id.ll_content);
                llContent.removeAllViews();
                ArrayList<NetBatchBean> inBatchList = inBatchListBean.inBatchList;
                boolean isEnable = false;
                if (inBatchList != null && !inBatchList.isEmpty()) {
                    for (NetBatchBean batchBean : inBatchList) {
                        View child = inflater.inflate(R.layout.out_car_item_batch, llContent, false);
                        ViewHolder childHolder = new ViewHolder(this, child);
                        childHolder.setText(R.id.tv_contact_name, Tools.getString(batchBean.contactName));
                        childHolder.setText(R.id.tv_under_company, Tools.getString(batchBean.underCompany));
                        childHolder.setText(R.id.tv_goods_type_info, Tools.getString(batchBean.goodsTypeInfo));
                        childHolder.setText(R.id.tv_goods_info, Tools.getString(batchBean.goodsInfo));
                        childHolder.setText(R.id.tv_fee2_id, Tools.getFee2Id(batchBean.fee2Id));
                        childHolder.setText(R.id.tv_fee3_id, Tools.getFee3Id(batchBean.fee3Id));
                        isEnable = isEnable || Tools.getEnable(batchBean.fee2Id, batchBean.fee3Id);
                        childHolder.setBackgroundColor(R.id.ll_fee, Tools.getStateColor(batchBean.fee2Id, batchBean.fee3Id));
                        llContent.addView(child);
                    }
                }
                if (isOutCar) {
                    holder.setText(R.id.tv_1, "临时出场");
                    holder.setText(R.id.tv_2, "查看车辆详情");
                    holder.setBackgroundRes(R.id.tv_1, R.color.celestial_blue);
                    holder.setBackgroundRes(R.id.tv_2, R.color.color_ed4a49);
                    if (isEnable) {
                        holder.getView(R.id.tv_3).setEnabled(false);
                        holder.setBackgroundRes(R.id.tv_3, R.color.color_e4e4e4);
                        holder.setText(R.id.tv_3, "允许离场");
                    } else {
                        if ("1".equals(incarInfo.outCarStatus)) {
                            holder.getView(R.id.tv_3).setEnabled(false);
                            holder.setBackgroundRes(R.id.tv_3, R.color.color_e4e4e4);
                            holder.setText(R.id.tv_3, "已离场");
                        } else {
                            holder.getView(R.id.tv_3).setEnabled(true);
                            holder.setBackgroundRes(R.id.tv_3, R.color.color_39ca74);
                            holder.setText(R.id.tv_3, "允许离场");
                        }
                    }
                    holder.setTextColorRes(R.id.tv_1, R.color.white);
                    holder.setTextColorRes(R.id.tv_2, R.color.white);
                    holder.setTextColorRes(R.id.tv_3, R.color.white);
                    holder.setOnClickListener(R.id.tv_1, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            temporaryOutCar(incarInfo.inCarId);
                        }
                    });
                    holder.setOnClickListener(R.id.tv_2, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            loadDetail();
                        }
                    });
                    holder.setOnClickListener(R.id.tv_3, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            outCar(incarInfo.inCarId);
                        }
                    });
                } else {
                    holder.setText(R.id.tv_1, "查看车辆详情");
                    holder.setText(R.id.tv_2, "补录未登记车辆");
                    if ("1".equals(incarInfo.superviseOther)) {
                        holder.getView(R.id.tv_3).setEnabled(false);
                        holder.setBackgroundRes(R.id.tv_3, R.color.color_e4e4e4);
                        holder.setText(R.id.tv_3, "已监察");
                    } else {
                        holder.getView(R.id.tv_3).setEnabled(true);
                        holder.setBackgroundRes(R.id.tv_3, R.color.celestial_blue);
                        holder.setText(R.id.tv_3, "监察无差异");
                    }
                    holder.setBackgroundRes(R.id.tv_1, R.color.color_ed4a49);
                    holder.setBackgroundRes(R.id.tv_2, R.color.color_d7);
                    holder.setTextColorRes(R.id.tv_1, R.color.white);
                    holder.setTextColorRes(R.id.tv_2, R.color.color_169999);
                    holder.setTextColorRes(R.id.tv_3, R.color.white);
                    holder.setOnClickListener(R.id.tv_1, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            loadDetail();
                        }
                    });
                    holder.setOnClickListener(R.id.tv_2, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            makeupCar();
                        }
                    });
                    holder.setOnClickListener(R.id.tv_3, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            supervisionCar(incarInfo.inCarId);
                        }
                    });
                }
            }
        }
    }

    private void supervisionCar(final String inCarId) {
        if (StringUtils.isEmpty(inCarId)) {
            ToastUtils.show("未找到该进场单！");
            return;
        }
        final AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("确定监察？")
                .setNegativeButton("取消", null)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mPresenter.supervisionCar(inCarId, "");
                    }
                }).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    private void loadDetail() {
        Intent intent = new Intent(this, OutCarBatchDetailActivity.class);
        intent.putExtra("queryId", queryId);
        intent.putExtra("isOutCar", isOutCar);
        startActivity(intent);
    }

    private void temporaryOutCar(final String inCarId) {
        if (StringUtils.isEmpty(inCarId)) {
            ToastUtils.show("未找到该进场单！");
            return;
        }
        final AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("确定临时离场？")
                .setNegativeButton("取消", null)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mPresenter.toOutCar(inCarId, "临时离场");
                    }
                }).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    private void makeupCar() {
        startActivity(new Intent(this, SupervisionMakeupCarActivity.class));
    }

    private void outCar(final String inCarId) {
        if (StringUtils.isEmpty(inCarId)) {
            ToastUtils.show("未找到该进场单！");
            return;
        }
        final AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("确定离场？")
                .setNegativeButton("取消", null)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mPresenter.toOutCar(inCarId, "");
                    }
                }).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    @Override
    public void loadInCarListSuccess(NetInBatchListBean inCarList) {
        setData(inCarList);
    }

    @Override
    public void toOutCarSuccess() {
        mPresenter.loadOutCarDetail(queryId);
    }

    @Override
    public void supervisionCarSuccess() {
        mPresenter.loadOutCarDetail(queryId);
    }
}
